﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodtotalServices : Repository<corecmsfoodtotal>, IcorecmsfoodtotalServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfoodtotal entity)
        {
            return base.Change<corecmsfoodtotal>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodtotal> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodtotal entity)
        {
            return base.Change<corecmsfoodtotal>().Update(a => new corecmsfoodtotal()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                foodId = entity.foodId,
                foodSpecId = entity.foodSpecId,
                qty = entity.qty,
                price = entity.price,
                minQty = entity.minQty,
                maxQty = entity.maxQty,
                updateTime = DateTime.Now
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodtotal> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodtotal>().Update(a => new corecmsfoodtotal()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    foodId = item.foodId,
                    foodSpecId = item.foodSpecId,
                    qty = item.qty,
                    price = item.price,
                    minQty = item.minQty,
                    maxQty = item.maxQty,
                    updateTime = DateTime.Now
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodtotal>().Delete(a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            //foreach (int id in ids)
            //{
            //    returnKey = base.Change<corecmsfoodtotal>().Update(a => new corecmsfoodtotal()
            //    {
            //        isDelete = true,
            //        updateTime = DateTime.Now
            //    }, a => a.id == id);
            //}
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodtotal> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodtotal> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodtotal> lst = new List<corecmsfoodtotal>();
            var where = Expressionable.Create<corecmsfoodtotal>();

            if (!string.IsNullOrEmpty(sName))
            {

            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }

            return lst;
        }
        #endregion

    }
}
